package com.AAA.controller.sas;

import com.AAA.Util.EasyExcelUtil;
import com.AAA.entity.LayData;
import com.AAA.entity.MyPage;
import com.AAA.entity.sas.TSas;
import com.AAA.service.Impl.mht.TmhtRecordDedtailServiceImpl;
import com.AAA.service.Impl.sas.TSasRecordDedtailServiceImpl;
import com.AAA.service.sas.ITSasService;
import com.AAA.service.Impl.sas.TSasServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("tSas")
public class TSasController {
    @Autowired
    private ITSasService service;
    @Autowired
    private TSasRecordDedtailServiceImpl serviceimpl;
    @PostMapping("addAll")
    @ResponseBody
    public LayData LayData (HttpServletRequest request, Model model, MultipartFile file) throws IOException {
        List<Object> objects = EasyExcelUtil.readExcel(file, new TSas(), 1, 1);
        int count=0;
        List<Integer> list=new ArrayList<>();
        LayData layData=new LayData();
        for (Object t:objects){
            TSas tSas= (TSas) t;
            count++;
            Integer addall = service.addAll(tSas);
            if (addall!=addall){
                list.add(count);
            }
        }
        if (list.size()==0){
            layData.setMsg("添加成功");
        }else {
            layData.setData("有第"+list.toString()+"条未添加成功");
        }
        return layData;
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public LayData queryall(MyPage myPage, TSas tSas){
        PageInfo pageInfo=service.query(myPage,tSas);
        return new LayData(pageInfo);
    }

    @PostMapping("add")
    @ResponseBody
    public LayData add(TSas tSas){
        if(tSas.getQid()==null){
            return new LayData(service.add(tSas));
        }else{
            return new LayData(service.edit(tSas));
        }
    }

    @PostMapping("del")
    @ResponseBody
    public LayData delete(Integer qid){
        TSas tSas = service.selectById(qid);
        Integer tid = tSas.getTid();
        boolean b = serviceimpl.selectByTid(tid);
        if(b){
            return new LayData(0);
        }else {
            boolean delete = service.delete(qid);
            return new LayData(delete );
        }
    }

    //-------------------------------------------------------------------------------

    // 批量导入数据
    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        service.dealFile(file);
        return new LayData(true);
    }

    @GetMapping
    @ResponseBody
    public LayData getAllSub(MyPage myPage){
        return new LayData(service.getAll(myPage));
    }

    //fpa做题页面
    @GetMapping("index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(30);
        myPage.setPage(1);
        PageInfo all = service.getAll(myPage);
        request.setAttribute("allsub",all);
        return "/sas/sasindex";
    }
}

